package com.nna88.voz.main;

import java.util.ArrayList;
import java.util.Iterator;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;

import com.nna88.voz.contain.containPage2;
import com.nna88.voz.listview.listViewCustom2;
import com.nna88.voz.listview.listViewCustom2.OnActionItemClickListener;

public class PageCP extends Page{
	final static String urlNewSubscribe = "http://vozforums.com/usercp.php";
	final static String urlListSubscribe= "http://vozforums.com/subscription.php?do=viewsubscription";
	private ArrayList<containPage2> ListContains;
	private listViewCustom2 adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		ListContains = new ArrayList<containPage2>();
		adapter = new listViewCustom2(mActivity,ListContains);
	    adapter.setSize(mTextSize);
	    mList.setAdapter(adapter);
	    mParser.setUrl(urlNewSubscribe);
	    mTask.execute(TASK_GETDOC);
	    
	    adapter.setOnActionItemClickListener(new OnActionItemClickListener() {
			
			@Override
			public void onItemClick(int pos, int actionId, int state) {
				// TODO Auto-generated method stub
				if(state == 0){
					Intent i = null;
					if(ListContains.get(pos).isUrl()==0){ //forum
						i = new Intent(mContext, Page2.class);
					}else	//thread									
						i = new Intent(mContext, Page3.class); 
					if(actionId == 1)	
						i.putExtra("URL", ListContains.get(pos).UrlThread());
					else if(actionId == 2) 
						i.putExtra("URL", ListContains.get(pos).UrlLastPosst());
					i.putExtra("TITLE", ListContains.get(pos).Thread());
					startActivity(i);
					overridePendingTransition(R.anim.rail, R.anim.rail);
				}else{
					toast("state,"+state);
				}
			}
		});
	}
	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		updateSetFont(mTextSize);
	}
	@Override
	protected void updateSetFont(float value) {
		// TODO Auto-generated method stub
		super.updateSetFont(value);
		adapter.setSize(value);
		adapter.notifyDataSetChanged();
	}
	@Override
	void Parser(int taskType) {
		// TODO Auto-generated method stub
		super.Parser(taskType);
		if(mParser.getUrl().equals(urlNewSubscribe)){
			ListContains.clear();
			Bitmap bitmap = null;
			String text1 = null,text2=null,text3=null,text4=null,text1_url1=null,text2_url2=null;
			Element elemain = doc.select("tbody[id=collapseobj_usercp_subthreads]").first();
			Elements eles = elemain.select("tr:has(td[id*=td_threads])");
			Iterator<Element> ite = eles.iterator();
			Element eleTag1;
			text1 = "New Subscribed Threads";
			ListContains.add(new containPage2(text1,text2,text3,text4,bitmap,text1_url1,text2_url2));
			while(ite.hasNext()){
				eleTag1 = ite.next();
				if( eleTag1.select("a[id*=thread_title").first()!= null){
					text1 = eleTag1.select("a[id*=thread_title").first().text();
					text1_url1 = eleTag1.select("a[id*=thread_title").first().attr("href");
				}
				if(eleTag1.select("td[class=alt2]:has(span)").first() != null){
					text2 = eleTag1.select("td[class=alt2]:has(span)").first() .text();
				}
				if(eleTag1.select("td[align=center]").first() != null){
					text3 = eleTag1.select("td[align=center]").get(0).text();
					text4 = eleTag1.select("td[align=center]").get(1).text();
				}
				if(eleTag1.select("img[id*=thread_statusicon]").first() != null){
					bitmap = loadBitmapAssert(eleTag1.select("img[id*=thread_statusicon]").first().attr("src")); 
				}
				if(eleTag1.select("a[id*=thread_gotonew]").first() != null){
					text2_url2 = eleTag1.select("a[id*=thread_gotonew]").first().attr("href");
				}
				ListContains.add(new containPage2(text1,text2,text3,text4,bitmap,text1_url1,text2_url2));
				adapter.notifyDataSetChanged();
			}
			mTask.cancel(true);
			mTask = new TaskGetHtml();
			mParser.setUrl(urlListSubscribe);
			mTask.execute(TASK_GETDOC);
		}else if(mParser.getUrl().equals(urlListSubscribe)){
			Bitmap bitmap = null;
			String text1 = null,text2=null,text3=null,text4=null,text1_url1=null,text2_url2=null;
			Element elemain = doc.select("form[name=form]").first();
			Elements eles = elemain.select("tr:has(td[id*=td_threads])");
			Iterator<Element> ite = eles.iterator();
			Element eleTag1;
			text1 = "List Subscriptions";
			ListContains.add(new containPage2(text1,text2,text3,text4,bitmap,text1_url1,text2_url2));
			while(ite.hasNext()){
				text2_url2 = null;
				eleTag1 = ite.next();
				if( eleTag1.select("a[id*=thread_title").first()!= null){
					text1 = eleTag1.select("a[id*=thread_title").first().text();
					text1_url1 = eleTag1.select("a[id*=thread_title").first().attr("href");
				}
				if(eleTag1.select("td[class=alt2]:has(span)").first() != null){
					text2 = eleTag1.select("td[class=alt2]:has(span)").first() .text();
				}
				if(eleTag1.select("td[align=center]").first() != null){
					text3 = eleTag1.select("td[align=center]").get(0).text();
					text4 = eleTag1.select("td[align=center]").get(1).text();
				}
				if(eleTag1.select("img[id*=thread_statusicon]").first() != null){
					bitmap = loadBitmapAssert(eleTag1.select("img[id*=thread_statusicon]").first().attr("src")); 
				}
				if(eleTag1.select("a[id*=thread_gotonew]").first() != null){
					text2_url2 = eleTag1.select("a[id*=thread_gotonew]").first().attr("href");
				}
				ListContains.add(new containPage2(text1,text2,text3,text4,bitmap,text1_url1,text2_url2));
				adapter.notifyDataSetChanged();
			}
		}
		
	}
}
